package com.dragon.client.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.finance.ICustomerPaymentService;
import com.dragon.repository.domain.dto.CustomerPaymentDTO;
import com.dragon.repository.domain.dto.PaymentContractDTO;
import com.dragon.repository.domain.dto.PlanCollectDTO;
import com.dragon.repository.domain.vo.CustomerPaymentVO;
import com.dragon.repository.entity.CustomerPayment;
import com.dragon.repository.entity.PaymentContract;
import com.dragon.repository.entity.PlanCollect;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 回款表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-03
 */
@RestController
@RequestMapping("customerPayment")
@Api(tags = "收款管理")
public class CustomerPaymentController {

    @Autowired
    ICustomerPaymentService service;

    @GetMapping("list")
    @ApiOperation("获取收款列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索内容", required = false, type = "string"),
            @ApiImplicitParam(name = "type", value = "回款类型1合同 2其他", required = false, type = "int"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = false, type = "int"),
            @ApiImplicitParam(name = "paymentStatus", value = "回款状态", required = false, type = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, type = "string")
    })
    public ResponseData<List<CustomerPaymentDTO>> paymentList(@RequestParam Integer page, @RequestParam Integer limit,
                                                              @RequestParam(required = false) String search,
                                                              @RequestParam(required = false) Integer type,
                                                              @RequestParam(required = false) Integer status,
                                                              @RequestParam(required = false) Integer paymentStatus,
                                                              @RequestParam(required = false) String startTime,
                                                              @RequestParam(required = false) String endTime){
        Page<CustomerPayment> paymentPage = service.paymentList(page, limit, search,type, status, paymentStatus, startTime,endTime);
        return ResponseData.success(CustomerPaymentDTO.convertList(paymentPage.getRecords()), paymentPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("添加收款")
    @Log
    //@AddPage
    public ResponseData addPayment(@RequestBody @Validated(Insert.class) CustomerPaymentVO vo){
        service.addPayment(CustomerPaymentVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("更新收款")
    public ResponseData updatePayment(@RequestBody @Validated(Update.class) CustomerPaymentVO vo ){
        service.updatePayment(CustomerPaymentVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除收款")
    public ResponseData deletePayment(@ApiParam(name = "id", value = "回款id", required = true, type = "int") @RequestParam Integer id){
        service.deletePayment(id);
        return ResponseData.success();
    }

    @PostMapping("listByPaymentId")
    @ApiOperation("根据收款id获取详情")
    public ResponseData listByPaymentId(@RequestParam
                                        @ApiParam(name = "id", value = "回款id", required = true, type = "int") Integer id){
        return ResponseData.success(service.detailById(id));
    }

    @PostMapping("getAmount")
    @ApiOperation("获取对应年月应收实收金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id",  required = true, type = " int "),
            @ApiImplicitParam(name = "year", value = "年份", required = false, type = "string"),
            @ApiImplicitParam(name = "yearMonth", value = "年月份", required = false, type = "string")
    })
    public ResponseData<Map<String,Object>> getAmount(@RequestParam Integer adminId,
                                                      @RequestParam(required = false) String year,
                                                      @RequestParam(required = false) String yearMonth) {
        return ResponseData.success(service.getAmount(adminId, year, yearMonth));

    }
    @GetMapping("getMoney")
    @ApiOperation(value = "获取在审金额")
    public ResponseData<List<PlanCollectDTO>> selectMoneyByPlanIdAndControllerId(
            @ApiParam(name = "planIds",required = true,type = "string",value = "计划id")
            @RequestParam String planIds){
        List<PlanCollect> planCollects = service.selectMoneyByPlanIdAndControllerId(planIds);
        return ResponseData.success(PlanCollectDTO.convertList(planCollects));
    }

    @GetMapping("billGetMoney")
    @ApiOperation(value = "账单获取在审金额")
    public ResponseData selectMoneyByPlanIdAndControllerId(
            @ApiParam(name = "billId",required = true,type = "string",value = "账单id")
            @RequestParam Integer billId){
        return ResponseData.success(service.selectMoneyByBillId(billId));
    }


    @PostMapping("getReceivablesMoney")
    @ApiOperation(value = "获取每年各月应收金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, type = "string")
    })
    public ResponseData getReceivablesMoney( @RequestParam Integer adminId ,
                                             @RequestParam String year){
        Map<String, Object> receivablesMoney = service.getReceivablesMoney(adminId, year);
        return ResponseData.success(receivablesMoney);
    }

    @PostMapping("getPaidMoney")
    @ApiOperation(value = "获取每年各月实收金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, type = "string"),
            @ApiImplicitParam(name = "type", value = "实收类型（1合同 2其他）", required = false, type = "int")
    })
    public ResponseData getPaid(@RequestParam Integer adminId,
                                @RequestParam String year,
                                @RequestParam(required = false) Integer type){
        Map<String, Object> paidMoney = service.getPaidMoney(adminId,year,type);
        return ResponseData.success(paidMoney);
    }

    @PostMapping("get")
    @ApiOperation(value = "获取实收占比")
    public ResponseData getReceivablesPercentage(Integer adminId, String year ,Integer type){
        Map<String, Object> map = service.getReceivablesPercentage(adminId, year, type);
        return ResponseData.success( map);
    }


    @GetMapping("planDetail")
    @ApiOperation(value = "应收账单收款明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId",required = true,type = "int",value = "应收主表id"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<PaymentContractDTO>> selectPlanCollectDetail(@RequestParam Integer planId,@RequestParam Integer page,@RequestParam Integer limit){
        Page<PaymentContract> paymentContractPage = service.selectPlanCollectDetail(planId, page, limit);
        return ResponseData.success(PaymentContractDTO.convertList(paymentContractPage.getRecords()),paymentContractPage.getTotal());
    }

    @GetMapping("customerDetail")
    @ApiOperation(value = "客户收款明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billId",required = true,type = "int",value = "账单主表id"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<CustomerPaymentDTO>> selectCustomerPaymentDetail(@RequestParam Integer billId,@RequestParam Integer page,@RequestParam Integer limit){

        Page<CustomerPayment> customerPaymentDTOPage = service.selectCustomerPaymentDetail(billId, page, limit);

        return ResponseData.success(customerPaymentDTOPage == null ? null : CustomerPaymentDTO.convertList(customerPaymentDTOPage.getRecords()),customerPaymentDTOPage == null ? 0 : customerPaymentDTOPage.getTotal());
    }


    @GetMapping("listStatistics")
    @ApiOperation(value = "列表统计")
    public ResponseData selectListStatistics(){
        return ResponseData.success(service.selectListStatistics());
    }

}
